package clients

import (
	"context"
	"strconv"

	"gitee.com/shiwjlinux/arm/apps/host"
)

// https://tehub.com/a/c8eNoj6z1S

var _ host.Service = (*Client)(nil)

func (c *Client) CreateHost(ctx context.Context, in *host.CreateHostRequest) (*host.Host, error) {
	h := host.DefaultHost()
	resp, err := c.C.R().
		SetContext(ctx).
		SetBody(in).
		SetResult(h).
		Post("/arm/api/v1/host/")
	if err != nil {
		return nil, err
	}

	// fmt.Printf("H %+v\n", resp)
	return resp.Result().(*host.Host), nil
}

func (c *Client) QueryHost(ctx context.Context, in *host.QueryHostRequest) (*host.HostSet, error) {
	set := host.NewHostSet()
	_, err := c.C.R().
		SetContext(ctx).
		SetResult(set).
		SetQueryParams(map[string]string{
			"sn":          in.Sn,
			"hostname":    in.Hostname,
			"page_number": strconv.Itoa(in.PageNumber),
			"page_size":   strconv.Itoa(in.PageSize),
		}).
		Get("/arm/api/v1/host/")
	if err != nil {
		return nil, err
	}

	// fmt.Printf("%+v\n", resp)
	return set, nil
}

// 更新用SN作为唯一标识
func (c *Client) UpdateHost(ctx context.Context, in *host.UpdateHostRequest) (*host.Host, error) {
	// c.logger.Debug().Msgf("(c *Client) UpdateHost %+v", in)
	h := host.DefaultHost()
	resp, err := c.C.R().
		SetContext(ctx).
		SetBody(in).
		SetResult(h).
		Put("/arm/api/v1/host/" + in.Id)
	if err != nil {
		return nil, err
	}

	// c.logger.Debug().Msgf("%+v", resp.Result())
	// c.logger.Debug().Msgf("%+v", resp.Body())
	// json.Unmarshal(resp.Body())
	// resp.Body()
	return resp.Result().(*host.Host), nil
}

func (c *Client) DeleteHost(ctx context.Context, in *host.DeleteHostRequest) (*host.Host, error) {
	c.C.R().SetContext(ctx).Delete("/arm/api/v1/host/" + in.Id)
	return nil, nil
}

func (c *Client) DescribeHost(context.Context, *host.DescribeHostRequest) (*host.Host, error) {
	return nil, nil
}
